package com.techsen.maven.plugins.version;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Pattern;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;

import com.techsen.maven.plugins.util.PomHelper;

@Mojo(name = "snapshot", aggregator = true)
public class SnapshotMojo extends AbstractScmMojo {
    
    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        if (this.hasParentProject()) {
            throw new MojoExecutionException("the goal version-maven-plugin:snapshot cannot used in child project");
        } else {
            Pattern releasePattern = Pattern.compile(this.releaseVersionPattern);
            if (!releasePattern.matcher(this.version).matches()) {
                throw new MojoExecutionException("the goal version-maven-plugin:release can only used in release version project");
            }
            int pos = version.lastIndexOf(".");
            String nextVersion = version;
            if (pos == -1) {
                try {
                    nextVersion = String.valueOf(Integer.parseInt(version) + 1);
                } catch (Exception e) {
                    nextVersion = version + ".1";
                }
            } else {
                String prefix = version.substring(0, pos);
                String suffix = version.substring(pos + 1);
                try {
                    nextVersion = prefix + "." + (Integer.parseInt(suffix) + 1);
                } catch (Exception e) {
                    nextVersion = version + ".1";
                }
            }
            nextVersion = nextVersion + "-SNAPSHOT";
            if (!this.useDefaultVersionUpdatePolicy) {
                System.out.print("please input next snapshot version, default [" + nextVersion + "]: ");
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                try {
                    String inputVersion = br.readLine().trim();
                    if (!"".equals(inputVersion)) {
                        nextVersion = inputVersion;
                    }
                    Pattern snapshotPattern = Pattern.compile(this.snapshotVersionPattern);
                    while (!snapshotPattern.matcher(nextVersion).matches()) {
                        System.out.println("snapshot version must match pattern: " + this.snapshotVersionPattern);
                        System.out.print("please input the valid snapshot version: ");
                        nextVersion = br.readLine().trim();
                    }
                    this.getLog().info("the new snapshot version is: " + nextVersion);
                } catch (IOException e) {
                    throw new MojoExecutionException("Cannot read from stdin");
                } finally {
                    try {
                        br.close();
                    } catch (IOException e) {
                    }
                }
            }

            File pomFile = new File(this.basedir, "pom.xml");
            File pomBakFile = new File(this.basedir, "pom-release.xml.bak");
            PomHelper pomhelper = new PomHelper(pomFile);
            pomhelper.write(pomBakFile);
            pomhelper.setVersion(nextVersion).write(pomFile);

            for (String module : pomhelper.getModules()) {
                File childPomFile = new File(this.basedir, module + "/pom.xml");
                File childPomBakFile = new File(this.basedir, module + "/pom-release.xml.bak");
                PomHelper childPomHelper = new PomHelper(childPomFile);
                childPomHelper.write(childPomBakFile);
                childPomHelper.setParentVersion(nextVersion).setVersion(nextVersion).write(childPomFile);
            }
        }
    }

}
